/**
 * 收银中心管理初始化
 */
var TOrderFee = {
    id: "TOrderFeeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TOrderFee.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {
            title: '运单单号', field: 'original_num', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if (d.order_num != null&&(d.audit_status==1||d.audit_status==3)) {
                    return '<span style="color: #00B83F;">' + d.original_num + '</span>'
                } else {
                    if(d.order_num==null){
                        return '<span style="color: #0a6aa1;">' + d.fee_name+ '</span>'
                    }
                    return d.original_num;
                }
            }
        },
        {title: '单据类型', field: 'modeType', visible: true, align: 'center', valign: 'middle'},
        {title: '交易对象', field: 'mm', visible: true, align: 'center', valign: 'middle'},
        {title: '交易对象身份', field: 'use_object', visible: true, align: 'center', valign: 'middle'},
        {title: '款项类型', field: 'fee_name', visible: true, align: 'center', valign: 'middle'},
        {title: '操作人', field: 'uname', visible: true, align: 'center', valign: 'middle'},
        {title: '金额', field: 'money', visible: true, align: 'center', valign: 'middle'},
        {title: '付款方式', field: 'pay_mode', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if (d.auditStatus =='未结算') {
                    return ''
                }
                return d.pay_mode;
            }
        },
        {title: '结算方式', field: 'settlementType', visible: true, align: 'center', valign: 'middle'},
        {title: '记账时间', field: 'create_time', visible: true, align: 'center', valign: 'middle'},
        {title: '状态', field: 'auditStatus', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
TOrderFee.check = function () {

    var selected = mTable.checkStatus(TOrderFee.id);
    // var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.data.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TOrderFee.seItem = selected.data[0];
        return true;
    }
};

/**
 * 检查是否选中
 */
TOrderFee.checks = function () {
    var selected = mTable.checkStatus(TOrderFee.id);
    if (selected.data.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }
    return true;
};

/**
 * 点击添加收银中心
 */
TOrderFee.openAddTOrderFee = function () {
    var index = layer.open({
        type: 2,
        shade: 0.01,
        title: '添加收银中心',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tOrderFeeOff/tOrderFee_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看收银中心详情
 */
TOrderFee.openTOrderFeeDetail = function () {

    var selected = mTable.checkStatus(TOrderFee.id);
    var ids = new Array();
    for (var i = 0; i < selected.data.length; i++) {
        if(selected.data[i].audit_status==1||selected.data[i].audit_status==2){
            ids.push(selected.data[i].id);
        }
    }

    if (this.check()) {
        if(ids.length==0){
            Feng.error("清单都已结算!");
            return;
        }
        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '收银核销',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tOrderFeeOff/tOrderFee_update?ids=' + ids.join(",")
        });
        this.layerIndex = index;
    }
};

/**
 * 删除收银中心
 */
TOrderFee.delete = function () {
    var selected = mTable.checkStatus(TOrderFee.id);
    var ids = new Array();
    for (var i = 0; i < selected.data.length; i++) {
        if(selected.data[i].audit_status==1||selected.data[i].audit_status==2){
            ids.push(selected.data[i].id);
        }
    }

    if (this.check()) {
        if(ids.length==0){
            Feng.error("清单都已结算!");
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/tOrderFeeOff/update", function (data) {
            Feng.success("标记成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("标记失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(","));
        ajax.set("status", 2);
        ajax.set("pay_mode", 0);
        ajax.start();
    }
};


TOrderFee.report = function () {
    $('#ys').text(0);
    $('#yf').text(0);
    var dj_type = $("#dj_type").val() + "";
    var feeType = $("#feeType").val() + "";
    var pname = $("#pname").val() + "";
    var auditStatus = $("#auditStatus").val() + "";
    var ajax = new $ax(Feng.ctxPath + "/tOrderFeeOff/listSum", function (data) {
        // if(data.length==2){
        //     $('#ys').text(data[0].money);
        //     $('#yf').text(data[1].money);
        // }
        // if(data.length==1){
        //     $('#ys').text(data[0].money);
        // }
        for (var i = 0; i < data.length; i++) {
            if (data[i].payments_type == 1) {
                $('#ys').text(data[i].money);
            } else {
                $('#yf').text(data[i].money);
            }
        }
    }, function (data) {

    });
    ajax.set("name", $("#name").val());
    ajax.set("beginTime", $("#beginTime").val());
    ajax.set("endTime", $("#endTime").val());
    ajax.set("dj_type", dj_type);
    ajax.set("pname", pname);
    ajax.set("feeType", feeType);
    ajax.set("auditStatus", auditStatus);
    ajax.start();

};

/**
 * 查询收银中心列表
 */
TOrderFee.search = function () {
    var dj_type = $("#dj_type").val() + "";
    var feeType = $("#feeType").val() + "";
    var pname = $("#pname").val() + "";
    var auditStatus = $("#auditStatus").val() + "";
    var payMode = $("#payMode").val() + "";
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['dj_type'] = dj_type;
    queryData['feeType'] = feeType;
    queryData['pname'] = pname;
    queryData['payMode'] = payMode;
    queryData['auditStatus'] = auditStatus;
    // alert( $("#dj_type").val());
    aTable.refresh(queryData);
    TOrderFee.report();
};


var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TOrderFee.initColumn();
    aTable = new LYTable(TOrderFee.id, "/tOrderFeeOff/list", defaultColunms);
    // table.setPaginationType("client");
    layui.use('table', function () {
        mTable = layui.table;
        TOrderFee.table = aTable.init(mTable);
    })

    $('#dj_type').selectpicker({
        style: 'btn btn-select',
        noneSelectedText: "单据类型"
    });
    $('#feeType').selectpicker({
        style: 'btn btn-select',
        noneSelectedText: "费用类型"
    });
    $('#auditStatus').selectpicker({
        style: 'btn btn-select',
        noneSelectedText: "结算类型"
    });
    $('#payMode').selectpicker({
        style: 'btn btn-select',
        noneSelectedText: "付款方式"
    });
    TOrderFee.report();
});
